--[[
     Ecchi Awesome WM theme 1.0
--]] 
local gears = require("gears")
local lain = require("lain")
local awful = require("awful")
local wibox = require("wibox")
local dpi = require("beautiful.xresources").apply_dpi

local os = os
local my_table = awful.util.table or gears.table -- 4.{0,1} compatibility

local colors = {
    black = "#131313",
    white = "#C6C6C6",
    yellow = "#b8c480",
    green = "#42e2b8",
    red = "#ff5a5f",
    charcoal = "#2f4452",
    periwinkle = "#cbd0ec",
    blue = "#617698",
    pine = "#6bab90",
    grape = "#5c4e90"
}

local theme = {}
theme.confdir = os.getenv("HOME") .. "/.config/awesome/themes/ecchi"
theme.font = "Noto Sans Regular 11"
theme.taglist_font = "Noto Sans Regular 13"
theme.menu_bg_normal = colors.black
theme.menu_bg_focus = colors.black
theme.bg_normal = colors.black
theme.bg_focus = colors.black
theme.bg_urgent = colors.black
theme.fg_normal = colors.white
theme.fg_focus = colors.pine
theme.fg_urgent = colors.red
theme.fg_minimize = colors.white
theme.border_width = dpi(2)
theme.border_normal = colors.charcoal
theme.border_focus = colors.grape
theme.border_marked = colors.pine
theme.menu_border_width = 0
theme.menu_height = dpi(25)
theme.menu_width = dpi(260)
theme.menu_fg_normal = colors.white
theme.menu_fg_focus = colors.pine
theme.menu_bg_normal = colors.black
theme.menu_bg_focus = colors.black
theme.tasklist_plain_task_name = true
theme.tasklist_disable_icon = true
theme.useless_gap = 5
theme.layout_tile = theme.confdir .. "/icons/tile.png"
theme.layout_tileleft = theme.confdir .. "/icons/tileleft.png"
theme.layout_tilebottom = theme.confdir .. "/icons/tilebottom.png"
theme.layout_tiletop = theme.confdir .. "/icons/tiletop.png"
theme.layout_fairv = theme.confdir .. "/icons/fairv.png"
theme.layout_fairh = theme.confdir .. "/icons/fairh.png"
theme.layout_spiral = theme.confdir .. "/icons/spiral.png"
theme.layout_dwindle = theme.confdir .. "/icons/dwindle.png"
theme.layout_max = theme.confdir .. "/icons/max.png"
theme.layout_fullscreen = theme.confdir .. "/icons/fullscreen.png"
theme.layout_magnifier = theme.confdir .. "/icons/magnifier.png"
theme.layout_floating = theme.confdir .. "/icons/floating.png"

local markup = lain.util.markup

local weather_na_text = "不明"

local sep = wibox.widget.textbox("   ")

-- Textclock
os.setlocale(os.getenv("LANG")) -- to localize the clock
local mytextclock = wibox.widget.textclock(
                        markup(colors.periwinkle, "🝰 ") ..
                            markup(colors.periwinkle, "%A %d %B ") ..
                            markup(colors.periwinkle, "%H:%M"))
mytextclock.font = theme.font

-- Calendar
theme.cal = lain.widget.cal({
    attach_to = {mytextclock},
    notification_preset = {
        font = "Noto Sans Mono Medium 10",
        fg = theme.fg_normal,
        bg = theme.bg_normal
    }
})

-- Weather
theme.weather = lain.widget.weather({
    APPID = "ADD YOUR APPID HERE",
    city_id = 3457191,
    notification_preset = {
        font = "Noto Sans Mono Medium 10",
        fg = theme.fg_normal
    },
    weather_na_markup = markup.fontfg(theme.font, colors.grape, weather_na_text),
    settings = function()
        icon = "☀ "
        units = math.floor(weather_now["main"]["temp"])

        if units <= 0 then
            tempcolor = colors.blue
        elseif units > 0 and units <= 16 then
            tempcolor = colors.green
        elseif units > 16 and units < 30 then
            tempcolor = colors.yellow
        else
            tempcolor = colors.red
        end

        widget:set_markup(markup.fontfg(theme.font, tempcolor,
                                        icon .. units .. "°C"))
    end
})

-- CPU
local cpu = lain.widget.cpu({
    settings = function()
        cpu_color = colors.green
        if cpu_now.usage > 40 and cpu_now.usage <= 65 then
            cpu_color = colors.yellow
        elseif cpu_now.usage > 65 then
            cpu_color = colors.red
        end

        icon = markup(cpu_color, "↺ ")

        widget:set_markup(markup.fontfg(theme.font, cpu_color,
                                        icon .. cpu_now.usage .. "%"))
    end
})

-- ALSA volume
theme.volume = lain.widget.alsa({
    settings = function()
        if volume_now.status == "off" then
            volume_now.level = "静く"
            volume_color = colors.red
            volume_icon = " "
        else
            volume_now.level = volume_now.level .. "%"
            volume_color = colors.blue
            volume_icon = "∿ "
        end

        icon = markup(volume_color, volume_icon)

        widget:set_markup(markup.fontfg(theme.font, volume_color,
                                        icon .. volume_now.level))
    end
})

-- Net
local netdowninfo = wibox.widget.textbox()
local netupinfo = lain.widget.net({
    settings = function()
        if iface ~= "network off" and
            string.match(theme.weather.widget.text, weather_na_text) then
            theme.weather.update()
        end

        down_icon = markup(colors.green, "↓ ")
        up_icon = markup(colors.red, "↑ ")

        widget:set_markup(markup.fontfg(theme.font, colors.red,
                                        up_icon .. net_now.sent))
        netdowninfo:set_markup(markup.fontfg(theme.font, colors.green,
                                             down_icon .. net_now.received))
    end
})

-- MEM
local memory = lain.widget.mem({
    settings = function()
        if mem_now.perc <= 25 then
            memcolor = colors.green
        elseif mem_now.perc > 25 and mem_now.perc <= 75 then
            memcolor = colors.yellow
        else
            memcolor = colors.red
        end

        icon = "⛶ "

        widget:set_markup(markup.fontfg(theme.font, memcolor,
                                        icon .. mem_now.perc .. "%"))
    end
})

function theme.at_screen_connect(s)
    -- Tags
    awful.tag(awful.util.tagnames, s, awful.layout.layouts[1])

    -- Create a promptbox for each screen
    s.mypromptbox = awful.widget.prompt()
    -- Create an imagebox widget which will contains an icon indicating which layout we're using.
    -- We need one layoutbox per screen.
    s.mylayoutbox = awful.widget.layoutbox(s)
    s.mylayoutbox:buttons(my_table.join(awful.button({}, 1, function()
        awful.layout.inc(1)
    end), awful.button({}, 3, function() awful.layout.inc(-1) end),
                                        awful.button({}, 4, function()
        awful.layout.inc(1)
    end), awful.button({}, 5, function() awful.layout.inc(-1) end)))
    -- Create a taglist widget
    s.mytaglist = awful.widget.taglist(s, awful.widget.taglist.filter.all,
                                       awful.util.taglist_buttons)

    -- Create a tasklist widget
    s.mytasklist = awful.widget.tasklist(s, awful.widget.tasklist.filter
                                             .currenttags,
                                         awful.util.tasklist_buttons)

    -- Create the wibox
    s.mywibox = awful.wibar({
        position = "top",
        screen = s,
        height = dpi(20),
        bg = theme.bg_normal,
        fg = colors.periwinkle
    })

    -- Add widgets to the wibox
    s.mywibox:setup{
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            layout = wibox.layout.fixed.horizontal,
            -- s.mylayoutbox,
            s.mytaglist,
            s.mypromptbox
        },
        -- s.mytasklist, -- Middle widget
        nil,
        { -- Right widgets
            layout = wibox.layout.fixed.horizontal,
            netdowninfo,
            sep,
            netupinfo.widget,
            sep,
            theme.volume.widget,
            sep,
            memory.widget,
            sep,
            cpu.widget,
            sep,
            theme.weather.widget,
            sep,
            mytextclock,
            sep,
            wibox.widget.systray()
        }
    }

    -- Create the bottom wibox
    s.mybottomwibox = awful.wibar({
        position = "bottom",
        screen = s,
        border_width = 0,
        height = dpi(20),
        bg = theme.bg_normal,
        fg = theme.fg_normal
    })

    -- Add widgets to the bottom wibox
    s.mybottomwibox:setup{
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            layout = wibox.layout.fixed.horizontal
        },
        s.mytasklist, -- Middle widget
        { -- Right widgets
            layout = wibox.layout.fixed.horizontal,
            s.mylayoutbox
        }
    }
end

return theme
